package com.sino.second.util;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.sino.util.StringUtils;

public class MailSendUtils {
		public static void sentMailHtml(MailSettingForm mailForm) throws Exception {
			String from = mailForm.getMailaddr();      //发件人地址
			System.out.println(from);
			String to = mailForm.getReceive();         //收件人
			String subject = mailForm.getSubject();    //主题
			String smtp = mailForm.getSmtp();          //smtp服务器地址
			String userName = mailForm.getMailaddr().substring(0,mailForm.getMailaddr().indexOf("@"));//用户名
			String passwd = mailForm.getPassword();//密码
			String content = mailForm.getContent();//邮件内容
			String body = "A text mail";
			String tfj2="";
			String tcontent="";
			String emailtype="text/html";
		    String filename;
			Properties props = System.getProperties();
			// 设置SMTP邮件服务器：
			props.put("mail.smtp.host", smtp);
			// SMTP服务器需要验证：
			props.put("mail.smtp.auth", "true");
			Multipart    mm=new    MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象 
            //设置信件文本内容     
			   BodyPart    mdp=new    MimeBodyPart();//新建一个存放信件内容的BodyPart对象     
			   mdp.setContent(content,emailtype+";charset=utf-8");//给BodyPart对象设置内容和格式/编码方式     
			   mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中     
			   if(mailForm.getFileList().size()>0){//有附件
				    for(String f:mailForm.getFileList()){
					   if(!"".equals(f)){
					   mdp=new    MimeBodyPart();
				       filename=f; //选择出每一个附件名   
	                   FileDataSource fds=new FileDataSource(filename); //得到数据源  
	                   int    filename1=filename.lastIndexOf("/");     
	    			   String    fname;
	    			   if(-1!=filename1)
	    			   {
	    				   fname=filename.substring(filename1+1);//提取文件名 
	    			   }
	    			   else{
	    				   fname=f;
	    			   }
	                   mdp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart   
	                   mdp.setFileName(MimeUtility.encodeText(fname,"GBK",null));  //得到文件名同样至入BodyPart   
	                   mm.addBodyPart(mdp);  
				   }
                } 
			   }
			// 传入用户名和口令：
			Session session = Session.getInstance(props,new PasswordAuthenticator(userName, passwd));
//			Session.getDefaultInstance(props,new PasswordAuthenticator(userName, passwd));
			// 创建新邮件：
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,false));
			msg.setSubject(subject);
			msg.setText(body);
			msg.setSentDate(new Date());
			//MimeMultipart multipart = new MimeMultipart();
		    //BodyPart msgBodyPart = new MimeBodyPart();
		    // 设置格式为"text/html"
		    //msgBodyPart.setContent(content, "text/html;charset=utf-8");
		   // BodyPart embedImage = new MimeBodyPart();
//		    DataSource ds = new URLDataSource(new URL("http://202.108.218.132/upload/2101/31378020080620153040.jpg"));
		   // embedImage.setDataHandler(new DataHandler(ds));
		    // 设置表头的内部ID,注意,所设置内容必须与前文对应,在此处,前文的引用为<img// src=\"cid:logo\"/>,因此Content-ID表头对应
		    // 的应该是<logo>
		    //embedImage.setHeader("Content-ID", "<logo>");
		    //multipart.addBodyPart(msgBodyPart);
		   // multipart.addBodyPart(embedImage);
		    msg.setContent(mm);
		    Transport.send(msg);
		}
		
		public static void main(String args[]) throws Exception{
			MailSendUtils s = new MailSendUtils();
			MailSettingForm mailForm = new MailSettingForm();
			mailForm.setContent("你好呀！！");
			mailForm.setMailaddr("huyuhong001@126.com");
			mailForm.setPassword("19821213");
			mailForm.setReceive("huyuhong001@126.com");
			mailForm.setSmtp("smtp.126.com");
			mailForm.setSubject("你好呀！");
			s.sentMailHtml(mailForm);
		}


}
